package com.df.happylearn.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.df.happylearn.R;
import com.df.happylearn.cache.CacheUtil;
import com.df.happylearn.model.Article;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHolder> {

    private Context mContext;
    private List<Article> mArticleList;

    public ArticleAdapter(Context context, List<Article> list) {
        mContext = context;
        mArticleList = list;
    }

    @Override
    public int getItemCount() {
        return mArticleList.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_article_list, parent, false);
        ViewHolder holder = new ViewHolder(view);
        ButterKnife.bind(holder, view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final Article articleInfo = mArticleList.get(position);
        // time
        setTime(holder, articleInfo, position);
        // head
        String url = articleInfo.getThumbnail();
        Glide.with(mContext)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.article_default)
                .into(holder.article_img);
        // title
        String[] title = articleInfo.getTitle().split("_");
        holder.title.setText(title[0]);
        // article source
        if (articleInfo.getTitle().contains("_")) {
            holder.source.setText(articleInfo.getOriginal());
        }
        // set read and unread state
        if (CacheUtil.getInstance(mContext).getBoolean(String.valueOf(articleInfo.getId()), false)) {
            holder.title.setTextColor(mContext.getResources().getColor(R.color.article_read));
        } else {
            holder.title.setTextColor(mContext.getResources().getColor(R.color.article_unread));
        }
        // register click event
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }
    }

    private void setTime(ViewHolder holder, Article articleInfo, int position) {
        Date date = new Date(Long.parseLong(articleInfo.getCtime()) * 1000);
        if (position == 0) {
            holder.ctime.setVisibility(View.VISIBLE);
            SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
            holder.ctime.setText(sdf.format(date));
        } else {
            Article next = mArticleList.get(position - 1);
            Date dateTwo = new Date(Long.parseLong(next.getCtime()) * 1000);
            if (date.getMonth() == dateTwo.getMonth() && date.getDay() == dateTwo.getDay()) {
                holder.ctime.setVisibility(View.GONE);
            } else {
                holder.ctime.setVisibility(View.VISIBLE);
                SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
                holder.ctime.setText(sdf.format(date));
            }
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.ctime)
        TextView ctime;
        @Bind(R.id.article_img)
        ImageView article_img;
        @Bind(R.id.title)
        TextView title;
        @Bind(R.id.sourse)
        TextView source;

        public ViewHolder(View view) {
            super(view);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickListener = mOnItemClickLitener;
    }
}
